import { IFeidaoAiMobile } from '@dfeidao/atom-mobile/interfaces';
import get from '@dfeidao/atom-mobile/local/get';
import nodejs from '@dfeidao/atom-mobile/msg/nodejs';
import service from '@dfeidao/atom-mobile/msg/service';
import alert from '@dfeidao/atom-mobile/msgbox/alert';
import toast from '@dfeidao/atom-mobile/msgbox/toast';
import back from '@dfeidao/atom-mobile/nav/back';
import { spaceid } from '../atom/config';
// import a001 from './a001';

export default async function a002(fd: IFeidaoAiMobile, e: Event, student_no: string) {
	const a = await alert('提示', '确定要签到？', ['确定', '取消']);
	if (a === 0) {
		const productid = get(fd, "productid");
		const class_id = get(fd, "class_id");
		const class_name = get(fd, "class_name");
		const teacher_no = get(fd, "teacher_no");
		const teacher_name = get(fd, "teacher_name");
		const arrive_time = new Date().getTime();
		const student_name = get(fd, "name") as string;

		const keshi = await nodejs<Array<{ hours_used: string, remaining_class_hours: string }>>('app-manual-check/s002', {
			student_no, productid
		});
		// console.log(keshi, '---------keshi-----------');
		if (keshi.length > 0 && Number(keshi[0].remaining_class_hours) === 0) {
			await toast({
				text: '课时为0,不能签到！',
				type: 'warning',
				position: 'center'
			});
			return;
		}
		let remaining_class_hours = 0;
		let hours_used = 0;
		if (keshi.length === 0) {
			const dd = get(fd, "class_hour");
			remaining_class_hours = Number(dd) - 2;
			hours_used = 2;
		} else {
			remaining_class_hours = Number(keshi[0].remaining_class_hours) - 2;
			hours_used = Number(keshi[0].hours_used) + 2;
		}

		const parent = await nodejs<Array<{ phone: string, parent_number: string, parent_name: string, student_name: string }>>('app-manual-check/s003', { student_no, productid });
		if (parent.length === 0) {
			await toast({
				text: '未获取到家长信息！',
				type: 'warning',
				position: 'center'
			});
			return;
		}
		// 获取家长手机号
		const parent_number = parent[0].parent_number;
		const parent_name = parent[0].parent_name;
		const phone = parent[0].phone;// "18738555317";
		const message = '签到成功！';
		const rew = await nodejs<{ code: number }>('app-manual-check/s004', {
			student_no,
			productid,
			teacher_no,
			teacher_name,
			student_name,
			parent_number,
			parent_name,
			arrive_time,
			remaining_class_hours,
			hours_used,
			class_id,
			class_name
		});
		// const rew = 'success';
		// console.log(rew);
		if (rew.code === 1) {
			// 推送消息 start;
			// 获取学生家长
			const openid = await nodejs<Array<{ openid: string }>>('app-manual-check/s005', { phone, productid });
			// openid
			if (openid.length === 0) {
				await toast({
					text: message + '未获取到对应openid',
					type: 'success',
					position: 'center'
				});
				// return a001(fd);
				back(fd);
			}
			// console.log(openid[0].openid);
			// 推送消息模板内容
			const information_template = await nodejs<Array<{ information_template_content: string }>>('app-manual-check/s006', { productid, information_template_type: "1", information_template_status: "1" });
			if (information_template.length === 0) {
				await toast({
					text: message + '无信息模板',
					type: 'success',
					position: 'center'
				});
				// return a001(fd);
				back(fd);
			}
			let information_template_content = information_template[0].information_template_content;
			information_template_content = information_template_content.replace("@", parent_name);
			information_template_content = information_template_content.replace("@", student_name);
			// console.log(information_template_content, '------模板内容----');

			const d = await service("thirdpart.weixinpublicno", {
				"serviceflag": "getaccesstoken",      // 固定参数
				"spaceid": spaceid,           // 当前项目的spaceid
				"thirdpay_type": "weixin_pubno", // 固定参数（weixin_pubno）
				"productid": productid    // 当前登录用户对应的productid（从系统参数params中获取）
			}) as { access_token: string };
			// console.log(d.access_token, '------d-------');
			try {
				const result = await service('thirdpart.weixinpubno.bulkpushmessage', {
					"tousers": [
						openid[0].openid/// res1中的openid
					],
					"spaceid": spaceid,
					"productid": productid,
					"access_token": d.access_token, /// 获取的token
					"custombody": {
						"msgtype": 'text', /// 推送的消
						'text': {
							'content': information_template_content
						}
					}
				}) as { code: number, msg: string };
				if (result.code > 0) {
					await toast({
						text: message + "已通知家长！",
						type: 'success',
						position: 'center'
					});
				}
			} catch (error) {
				await toast({
					text: message + '推送接口异常',
					type: 'success',
					position: 'center'
				});
			}
			// await a001(fd);
			back(fd);
		} else {
			await toast({
				text: '签到失败！',
				type: 'danger',
				position: 'center'
			});
		}
	}
}
